package com.xunqi.permission.controller;

import com.google.common.collect.Maps;
import com.xunqi.permission.beans.PageQuery;
import com.xunqi.permission.beans.PageResult;
import com.xunqi.permission.common.JsonData;
import com.xunqi.permission.param.DeptParam;
import com.xunqi.permission.param.UserParam;
import com.xunqi.permission.service.SysRoleService;
import com.xunqi.permission.service.SysTreeService;
import com.xunqi.permission.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

@Api(tags = "用户控制器")
@Controller
@RequestMapping(value = "sys/user")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysTreeService sysTreeService;
    @Resource
    private SysRoleService sysRoleService;

    @ApiOperation(value = "跳转到无权限页面",httpMethod = "GET",protocols = "HTTP",produces = "/noAuth.page",response = String.class)
    @RequestMapping(value = "/noAuth.page")
    public String noAuth() {
        return "noAuth";
    }

    @ApiOperation(value = "用户新增功能",httpMethod = "POST",protocols = "HTTP",produces = "/save.json",response = JsonData.class)
    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData saveUser(UserParam param) {
        sysUserService.save(param);
        return JsonData.success();
    }

    @ApiOperation(value = "用户修改功能",httpMethod = "POST",protocols = "HTTP",produces = "/update.json",response = JsonData.class)
    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData updateUser(UserParam param) {
        sysUserService.update(param);
        return JsonData.success();
    }


    @ApiOperation(value = "用户列表分页显示",httpMethod = "GET",protocols = "HTTP",produces = "/page.json",response = JsonData.class)
    @RequestMapping(value = "/page.json")
    @ResponseBody
    public JsonData page(@RequestParam("deptId")Integer deptId, PageQuery pageQuery) {
        PageResult result = sysUserService.getPageByDeptId(deptId,pageQuery);
        return JsonData.success(result);
    }


    @ApiOperation(value = "查看分配给用户的权限",httpMethod = "GET",protocols = "HTTP",produces = "/acls.json",response = JsonData.class)
    @RequestMapping(value = "/acls.json")
    @ResponseBody
    public JsonData acls(@RequestParam("userId") int userId) {
        Map<String,Object> map = Maps.newHashMap();
        map.put("acls",sysTreeService.userAclTree(userId));
        map.put("roles",sysRoleService.getRoleListByUserById(userId));
        return JsonData.success(map);
    }
}
